home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / STUTTGART / TEMP / GNU / flex / Examples < prev    next >
Text File  |  1995-06-28  |  3KB  |  121 lines

  1. Examples
  2. Previous: <Description=>Descriptio> * Next: <Format=>Format> * Up: <Top=>!Root>
  3.  
  4. #Wrap on
  5. {fH3}Some simple examples{f}
  6.  
  7. First some simple examples to get the flavor of how one
  8. uses {fCode}flex{f}.  The following {fCode}flex{f} input specifies a scanner
  9. which whenever it encounters the string "username" will
  10. replace it with the user's login name:
  11.  
  12. #Wrap off
  13. #fCode
  14. %%
  15. username    printf( "%s", getlogin() );
  16. #f
  17. #Wrap on
  18.  
  19. By default, any text not matched by a {fCode}flex{f} scanner is
  20. copied to the output, so the net effect of this scanner is
  21. to copy its input file to its output with each occurrence
  22. of "username" expanded.  In this input, there is just one
  23. rule.  "username" is the {fStrong}pattern{f} and the "printf" is the
  24. {fStrong}action{f}.  The "%%" marks the beginning of the rules.
  25.  
  26. Here's another simple example:
  27.  
  28. #Wrap off
  29. #fCode
  30.         int num\_lines = 0, num\_chars = 0;
  31.  
  32. %%
  33. \\n      ++num\_lines; ++num\_chars;
  34. .       ++num\_chars;
  35.  
  36. %%
  37. main()
  38.         \{
  39.         yylex();
  40.         printf( "\# of lines = %d, \# of chars = %d\\n",
  41.                 num\_lines, num\_chars );
  42.         \}
  43. #f
  44. #Wrap on
  45.  
  46. This scanner counts the number of characters and the
  47. number of lines in its input (it produces no output other
  48. than the final report on the counts).  The first line
  49. declares two globals, "num\_lines" and "num\_chars", which
  50. are accessible both inside {fEmphasis}yylex(){f} and in the {fEmphasis}main(){f}
  51. routine declared after the second "%%".  There are two rules,
  52. one which matches a newline ("\\n") and increments both the
  53. line count and the character count, and one which matches
  54. any character other than a newline (indicated by the "."
  55. regular expression).
  56.  
  57. A somewhat more complicated example:
  58.  
  59. #Wrap off
  60. #fCode
  61. \/\* scanner for a toy Pascal-like language \*\/
  62.  
  63. %\{
  64. \/\* need this for the call to atof() below \*\/
  65. \#include <math.h>
  66. %\}
  67.  
  68. DIGIT    [0-9]
  69. ID       [a-z][a-z0-9]\*
  70.  
  71. %%
  72.  
  73. \{DIGIT\}+    \{
  74.             printf( "An integer: %s (%d)\\n", yytext,
  75.                     atoi( yytext ) );
  76.             \}
  77.  
  78. \{DIGIT\}+"."\{DIGIT\}\*        \{
  79.             printf( "A float: %s (%g)\\n", yytext,
  80.                     atof( yytext ) );
  81.             \}
  82.  
  83. if|then|begin|end|procedure|function        \{
  84.             printf( "A keyword: %s\\n", yytext );
  85.             \}
  86.  
  87. \{ID\}        printf( "An identifier: %s\\n", yytext );
  88.  
  89. "+"|"-"|"\*"|"\/"   printf( "An operator: %s\\n", yytext );
  90.  
  91. "\{"[^\}\\n]\*"\}"     \/\* eat up one-line comments \*\/
  92.  
  93. [ \\t\\n]+          \/\* eat up whitespace \*\/
  94.  
  95. .           printf( "Unrecognized character: %s\\n", yytext );
  96.  
  97. %%
  98.  
  99. main( argc, argv )
  100. int argc;
  101. char \*\*argv;
  102.     \{
  103.     ++argv, --argc;  \/\* skip over program name \*\/
  104.     if ( argc > 0 )
  105.             yyin = fopen( argv[0], "r" );
  106.     else
  107.             yyin = stdin;
  108.  
  109.     yylex();
  110.     \}
  111. #f
  112. #Wrap on
  113.  
  114. This is the beginnings of a simple scanner for a language
  115. like Pascal.  It identifies different types of {fStrong}tokens{f} and
  116. reports on what it has seen.
  117.  
  118. The details of this example will be explained in the
  119. following sections.
  120.  
  121.